//
//  SinaOAuthController.m
//  NewsReader
//
//  Created by blacksun on 11-3-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "WeiboOAuthController.h"
#import "FeedDetailViewController.h"


@implementation WeiboOAuthController
@synthesize weibo;
@synthesize webView;
@synthesize parentContrpller;
@synthesize activityView;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//self.view.backgroundColor = [UIColor whiteColor];
	[self.navigationController setNavigationBarHidden:NO];
    
    UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 460)];
	aWebView.scalesPageToFit = YES;
	aWebView.delegate = self;
    self.webView = aWebView;
	[self.view addSubview:aWebView];
    [aWebView release];
    
    
    UIActivityIndicatorView *temp = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145.0, 165.0, 30.0, 30.0)];//[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    temp.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.view addSubview:temp];
    temp.hidesWhenStopped = YES;
    self.activityView = temp;
    [temp startAnimating];
    [temp release];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self performSelector:@selector(buildOAuth) withObject:nil afterDelay:0.2];
}

- (void)buildOAuth{
    //获取request token
	[weibo getRequestToken];
    
    
	//跳转到authorize页面
	[self.webView loadRequest:[self.weibo getAuthorizaRequest]];
    
    //[self.view bringSubviewToFront:self.activityView];

}


-(void)viewWillDisappear:(BOOL)animated{
	[self.webView stopLoading];
	self.webView = nil;
	[super viewWillDisappear:animated];
	[self.navigationController setNavigationBarHidden:YES animated:NO];
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    self.webView = nil;
    self.activityView = nil;
	self.weibo = nil;
    [super dealloc];
}

#pragma mark -
#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
	
	NSString *query = [[request URL] query];
	
	NSLog(@"--%@",query);
	
	[weibo parseOAuthVerifier:query];
	
	//获取access token
	if (weibo.oauthKey.verify && ![weibo.oauthKey.verify isEqualToString:@""]) {
		
		
		[weibo getAccessToken];
        [weibo getUserName];
        
        [weibo saveAccessToken];
		
		[parentContrpller performSelector:@selector(addSendTweetView:) withObject:self.weibo afterDelay:0.2];
		
		[self.navigationController popViewControllerAnimated:YES];
		
		
		return NO;
	}
	
	return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [self.activityView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.activityView stopAnimating];
}



@end
